//
//  AddressManageVC.m
//  digitalCurrency
//
//  Created by 小震GG on 2019/11/6.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "AddressManageVC.h"
#import "AddressManageCell.h"
#import "JHUIAlertView.h"
#import "NewAllNetManager.h"
@interface AddressManageVC ()
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextField *codeTF;
@property (nonatomic, strong) JHUIAlertView *alertView;
@property (nonatomic, copy) NSString *idStr;

@property (nonatomic, strong) LYEmptyView *emptyView;

@end

@implementation AddressManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewTitle.text = LocalizationKey(@"提币地址记录");
    
    self.dataArr = [[NSMutableArray alloc]init];
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor = [UIColor clearColor];
    [self headRefreshWithScrollerView:self.tableView];
    
    [self loadData];
}

- (void)loadData{
 
    [NewAllNetManager addressManage:@"1" pageSizeStr:@"80" CompleteHandle:^(id  _Nonnull resPonseObj, int code) {
       if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            if (codeInt == 0) {
                self.dataArr = [[NSMutableArray alloc]init];
                self.dataArr = resPonseObj[@"data"][@"list"];
               
                if (self.dataArr.count == 0) {
                    self.tableView.ly_emptyView = self.emptyView;
                }
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
           [self.tableView reloadData];
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark-下拉刷新数据
- (void)refreshHeaderAction {
    [self loadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 158;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self dataArr] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    AddressManageCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"id"];
    if (cell == nil) {
        cell = [[AddressManageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
    }
    
    NSDictionary *dataDic = self.dataArr[indexPath.row];
    
    cell.nameLB.text = [NSString stringWithFormat:@"%@",dataDic[@"coinName"]];
    cell.addressLB.text = [NSString stringWithFormat:@"%@",dataDic[@"address"]];
    cell.noteLB.text = [NSString stringWithFormat:@"%@",dataDic[@"remarks"]];
    
    cell.deleteBtn.tag = 1000+indexPath.row;
    cell.editBtn.tag = 2000+indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    [cell.editBtn addTarget:self action:@selector(editBtnAct:) forControlEvents:UIControlEventTouchUpInside];

    
    return cell;
}

- (void)editBtnAct:(UIButton *)sender{
    
    NSDictionary *dataDic = self.dataArr[sender.tag - 2000];
    self.idStr = [NSString stringWithFormat:@"%@",dataDic[@"id"]];
    
    JHUIAlertConfig *config = [[JHUIAlertConfig alloc] init];
    
    config.title.text  = LocalizationKey(@"修改地址信息");
    config.title.color = [UIColor colorWithHEX:maincolor];
    config.title.bottomPadding = 100;
    config.dismissWhenTapOut   = NO;
    config.contentViewWidth    = SCREEN_WIDTH*0.8;
    config.contentViewHeight   = 180+80;
    self.alertView = [[JHUIAlertView alloc] initWithConfig:config];
            
    __weak typeof(self)weakself = self;
    [self.alertView addCustomView:^(JHUIAlertView *alertView, CGRect contentViewRect, CGRect titleLabelRect, CGRect contentLabelRect) {
        
        UIButton *closebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closebtn.tintColor = [UIColor blackColor];
        [closebtn setImage:[UIImage imageNamed:@"loginClose"] forState:UIControlStateNormal];
        [closebtn addTarget:weakself action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
        closebtn.frame = CGRectMake(contentViewRect.size.width-30, 10, 20, 20);
        [alertView.contentView addSubview:closebtn];
        
        UIView *textBgView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-30, 44)];
        textBgView.backgroundColor = kColor(@"EAEAEA");
        [alertView.contentView addSubview:textBgView];
        textBgView.layer.cornerRadius = 5;
        textBgView.layer.masksToBounds = YES;
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(22, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-44, 44)];
        textField.backgroundColor = [UIColor colorWithHEX:@"EAEAEA"];
        textField.layer.cornerRadius = 5;
        textField.placeholder = LocalizationKey(@"修改钱包地址");
        [alertView.contentView addSubview:textField];
        weakself.textField = textField;
        
        UIView *codeBgView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabelRect)+88, contentViewRect.size.width-30, 44)];
        codeBgView.backgroundColor = kColor(@"EAEAEA");
        [alertView.contentView addSubview:codeBgView];
        codeBgView.layer.cornerRadius = 5;
        codeBgView.layer.masksToBounds = YES;
        
        UITextField *codeTF = [[UITextField alloc] initWithFrame:CGRectMake(22, CGRectGetMaxY(titleLabelRect)+88, contentViewRect.size.width-44-80, 44)];
        codeTF.backgroundColor = [UIColor colorWithHEX:@"EAEAEA"];
        codeTF.layer.cornerRadius = 5;
        codeTF.keyboardType = UIKeyboardTypeNumberPad;
        codeTF.placeholder = LocalizationKey(@"修改备注信息");
        [alertView.contentView addSubview:codeTF];
        weakself.codeTF = codeTF;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(15, CGRectGetMaxY(textField.frame)+88, contentViewRect.size.width-30, 44);
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitle:LocalizationKey(@"Confirm") forState:0];
        [button setTitleColor:[UIColor whiteColor] forState:0];
        button.layer.cornerRadius = 5;
        button.backgroundColor = [UIColor colorWithHEX:maincolor];
        [button addTarget:weakself action:@selector(submitAll) forControlEvents:1<<6];
        [alertView.contentView addSubview:button];
    }];
    
    [self.view addSubview:self.alertView];
    
    self.textField.text = [NSString stringWithFormat:@"%@",dataDic[@"address"]];
    self.codeTF.text = [NSString stringWithFormat:@"%@",dataDic[@"remarks"]];
}

- (void)closeView {
    [self.alertView dismiss];
    [self.view endEditing:YES];
    self.textField.text = @"";
    self.codeTF.text = @"";
}

- (void)submitAll {
//    NSString *code = self.codeTF.text;
//    NSString *pwd = self.textField.text;
    
    [NewAllNetManager editAddressManage:self.idStr addressStr:self.textField.text remarksStr:self.codeTF.text CompleteHandle:^(id  _Nonnull resPonseObj, int code) {
        [self closeView];
       if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            if (codeInt == 0) {
                [self loadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
//
//    if (pwd.length == 0) {
//        [self.view makeToast:LocalizationKey(@"inputMoneyPassword") duration:1.5 position:CSToastPositionCenter];
//        return;
//    }
//    __weak typeof(self)weakself = self;
//    [EasyShowLodingView showLodingText:LocalizationKey(@"loading")];
//    NSDictionary *dic = @{@"userId":[YLUserInfo shareUserInfo].ID,@"coinId":self.unit,@"value":amont,@"jyPassword":self.textField.text};
//    [HomeNetManager doFinancingsDrawoutWithInfo:dic CompleteHandle:^(id resPonseObj, int code) {
//
//        weakself.amountTextField.text = @"";
//        [EasyShowLodingView hidenLoding];
//        NSLog(@"resPonseObj:%@",resPonseObj);
//        NSInteger codeInt = [resPonseObj[@"code"] integerValue];
//        if (code) {
//            if (codeInt == 0) {
//
//                [[NSUserDefaults standardUserDefaults] setObject:amont forKey:@"financeCnt"];
//                [[NSUserDefaults standardUserDefaults] synchronize];
//
//                [weakself.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [weakself.navigationController popViewControllerAnimated:YES];
//                });
//            }
//            else if (codeInt == -1002 ){
//                [ShowLoGinVC showLoginVc:self withTipMessage:resPonseObj[MESSAGE]];
//            }
//            else {
//                [weakself.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
//            }
//        }
//        else {
//            [weakself.view makeToast:[[ChangeLanguage bundle] localizedStringForKey:@"noNetworkStatus" value:nil table:@"English"] duration:1.5 position:CSToastPositionCenter];
//        }
//    }];
}

- (void)deleteBtnAct:(UIButton *)sender{
    NSDictionary *dataDic = self.dataArr[sender.tag - 1000];
    self.idStr = [NSString stringWithFormat:@"%@",dataDic[@"id"]];
    
    NSString *title = LocalizationKey(@"删除");
    NSString *message = LocalizationKey(@" 是否删除地址?");
    NSString *cancelButtonTitle = LocalizationKey(@"取消");
    NSString *otherButtonTitle = LocalizationKey(@"确定");
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消.");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"确定.");
        
        [self setDeleteData];

    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)setDeleteData
{
    [NewAllNetManager deleteAddressManage:self.idStr CompleteHandle:^(id  _Nonnull resPonseObj, int code) {
       if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            if (codeInt == 0) {
                [self loadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
        _emptyView.contentViewY = SCREEN_HEIGHT/2.0-100;
    }
    return _emptyView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
